﻿@using AntDesign.Core.JsInterop.Modules.Components
@using System.ComponentModel.DataAnnotations
@using System.Text.Json
@inherits AntDesignTestBase
@code {
    enum City
    {
        [Display(Name = "Shanghai City")]
        Shanghai,
        Zhejiang,
        Beijing,
    }

    public Select_Render_Tests()
    {
        JSInterop.Setup<AntDesign.JsInterop.DomRect>(JSInteropConstants.GetBoundingClientRect, _ => true)
                    .SetResult(new AntDesign.JsInterop.DomRect());  
    }

    [Fact]
    public void Render_enum_select() 
    {
         //Arrange
            
        var cut = Render<EnumSelect<City>>(
    @<EnumSelect TEnum="City"></EnumSelect>
    );
        var options = cut.FindAll(".ant-select-item-option-content");

        Assert.Equal(3, options.Count);
        Assert.Equal("Shanghai City", options[0].TextContent.Trim('\n').Trim());
        Assert.Equal("Zhejiang", options[1].TextContent.Trim());
        Assert.Equal("Beijing", options[2].TextContent.Trim());
    }

    [Fact] //covers scenario from issue #1989
    public async Task Render_select_dropdown_on_arrow_click()
    {
		#if !NET6_0_OR_GREATER
			JSInterop.SetupVoid(JSInteropConstants.Focus, _ => true).SetVoidResult();;
		#endif
        JSInterop.Setup<OverlayPosition>(JSInteropConstants.OverlayComponentHelper.AddOverlayToContainer, _ => true)
            .SetResult(new OverlayPosition() { Top = 0, Left = 0, ZIndex = 5000, Placement = Placement.BottomLeft });        

        var mock = base.MockedDomEventListener;
        Action<JsonElement>? onMouseUpCallback = default;
        mock.Setup(s => s.AddShared<JsonElement>("document", "mouseup", It.IsAny<Action<JsonElement>>(), It.IsAny<bool>()))
            .Callback((object dom, string eventName, Action<JsonElement> callback, bool preventDefault) =>
                {                
                    if (callback.Target is AntDesign.Internal.OverlayTrigger)
                    {
                        onMouseUpCallback = callback;                
                    }
                });

        var plannedInvocation = JSInterop.Setup<string>(JSInteropConstants.AddDomEventListener, _ => true);
        var cut = Render<EnumSelect<City>>(@<EnumSelect TEnum="City"></EnumSelect>);
        //Act
        var arrow = cut.Find("span.ant-select-arrow");
        onMouseUpCallback.Should().NotBeNull();
        
        await cut.InvokeAsync(() => arrow.Click());
        cut.WaitForState(() => !cut.Find("div.ant-select-dropdown").ClassList.Contains("ant-select-dropdown-hidden"));
        await cut.InvokeAsync(() => onMouseUpCallback?.Invoke(default));
        //This is probably not the best solution. However this test is awkward - we are testing
        //for something that should not appear. In failing scenarios it appears after some
        //time (due to multiple async/await and re-renderings). That actually points to
        //a probable wrong design in the way the OverlayTrigger & Overlay are functioning.
        //So here we wait and hope the delay is going be long enough for the component
        //to finish all its renderings. 
        await Task.Delay(500); 
        //Assert
        var dropdown = cut.Find("div.ant-select-dropdown");
        dropdown.ClassList.Should().NotContain("ant-select-dropdown-hidden");
    }
}